<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctx}/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctx}/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">书名</label>
        <div class="layui-input-block">
            <input type="text" name="bookName" lay-verify="required" lay-reqtext="书名不能为空" placeholder="请输入书名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">作者</label>
        <div class="layui-input-block">
            <input type="text" name="bookAuthor" lay-verify="required" lay-reqtext="作者不能为空" placeholder="请输入作者" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">类型</label>
        <div class="layui-input-block">
            <input type="text" name="dictionaryValue" lay-verify="required" lay-reqtext="类型不能为空" placeholder="请输入类型" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="number" name="bookPrice" placeholder="请输入价格" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">语言</label>
        <div class="layui-input-block">
            <input type="text" name="language" placeholder="请输入语言" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出版时间</label>
        <div class="layui-input-block">
            <input type="datetime-local" name="publishDate" placeholder="请输入出版时间" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出版商</label>
        <div class="layui-input-block">
            <input type="text" name="publisher" placeholder="请输入出版商" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <textarea name="introduction" class="layui-textarea" placeholder="请输入书籍简介"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            <button id="closeBtn" class="layui-btn layui-btn-normal">取消提交</button>
        </div>
    </div>
</div>
<script src="${ctx}/lib/layui/layui.js" charset="utf-8"></script>
<script>
    const ctx = "${ctx}";
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data);
            $.ajax({
                type: "post",
                url: ctx + "/book/add",
                data: {
                    bookName: data.field.bookName,
                    bookAuthor: data.field.bookAuthor,
                    dictionaryValue: data.field.dictionaryValue,
                    bookPrice: data.field.bookPrice,
                    language: data.field.language,
                    publishDate: data.field.publishDate,
                    publisher: data.field.publisher,
                    introduction: data.field.introduction
                },
                success: function (data) {
                    if (data.code === 200) {
                        layer.msg("操作成功",function () {
                            const iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            parent.location.reload();
                        })
                    } else {
                        layer.msg(data.msg);
                    }
                }

            })

            return false;
        });

        /**
         * 关闭弹出层
         */
        $("#closeBtn").click(function () {
            // 先得到当前iframe层的索引
            var index = parent.layer.getFrameIndex(window.name);
            // 再执⾏关闭
            parent.layer.close(index);
        });
    });
</script>
</body>
</html>